﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;
using PPAP.Model;

namespace PPAP.Web.Part
{
    public partial class SamplingReport : PageBase
    {
        PPARBO pbo = new PPARBO();
        SamplecardBO sbo = new SamplecardBO();
        T_FileBO manager = new T_FileBO();
        public string rid { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["rid"] != null)
                {
                    rid = Request.QueryString["rid"].ToString();
                    DataSet ds = pbo.GetReportByPPARid(Request.QueryString["rid"].ToString());
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr = ds.Tables[0].Rows[0];
                        DataSet dsfile = manager.GetFileList(dr["DEVIATIONSHEET"].ToString());
                        //lblDecision.Text = dr["decisiontext"].ToString();
                        // lblExplanations.Text = dr["explanations"].ToString()==""?dr["remarks"].ToString():dr["explanations"].ToString();
                        lblQe.Text = dr["quanlityengineer"].ToString();
                        lblQM.Text = dr["quanlitymanager"].ToString();
                        lblQVP.Text = dr["quanlityvp"].ToString();
                        lblSamplingReportNo.Text = dr["samplingreportno"].ToString();
                        lblSupplier.Text = dr["suppliername"].ToString();

                        DataSet ds2 = sbo.GetList(" s.pparid=" + Request.QueryString["rid"].ToString() + " ");
                        foreach (DataRow drr in ds2.Tables[0].Rows)
                        {
                            if (drr["RESULT"].ToString() == "Y" || drr["RESULT"].ToString() == "R")
                            {
                                lblExplanations.Text += drr["testitemname"].ToString() + "\n" + drr["testitemname"].ToString() + " is not available for BDNT. Reason for deviation is " + drr["reason"].ToString() + "\n\n";
                            }
                        }

                        if (dr["elevel"].ToString() == "E")
                        {
                            lblDecision.Text = "No series delivery release can be issued.";
                        }
                        else
                        {
                            if (dr["finalresult"].ToString() == "G")
                            {
                                lblDecision.Text = "The series delivery release has been issued";
                            }
                            if (dr["finalresult"].ToString() == "Y")
                            {
                                lblDecision.Text = "Only a conditional series delivery release has been issued";
                            }
                            if (dr["finalresult"].ToString() == "R")
                            {
                                lblDecision.Text = "The series delivery release has not been issued";
                            }
                        }
                        if (dsfile.Tables[0].Rows.Count > 0&&DateTime.Parse(dr["resubmissiondate"].ToString())>DateTime.Parse("1900-02-01"))
                        {
                            lblDecision.Text += "We expect an optimized product and/or process as well as a new PPA-Report latest till the re-submission date "+DateTime.Parse(dr["resubmissiondate"].ToString()).ToString("yyyy-MM-dd");
                        }
                    }

                    lblDecision.Enabled = false;
                    lblExplanations.Enabled = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["rid"] != null)
            {
                PPAR p = pbo.GetModel(int.Parse(Request.QueryString["rid"].ToString()));
                if (p != null)
                {
                    p.EXPLANATIONS = lblExplanations.Text;
                    pbo.Update(p);
                }
            }
        }
    }
}